﻿using Microsoft.AspNetCore.Mvc;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class DWController : Controller
	{
		DWRepository dwRepository = new DWRepository();


		/// <summary>
		/// 获取计量单位
		/// </summary>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetDW")]
		public IActionResult GetDW(int state, string? str)
		{
			List<DW> dw = dwRepository.GetDW(state, str);
			return Ok(dw);
		}


		/// <summary>
		/// 绑定单位下拉框
		/// </summary>
		/// <returns></returns>
		[HttpGet("GetOneDW")]
		public IActionResult GetOneDW()
		{
			List<DW> dw = dwRepository.GetOneDW();
			return Ok(dw);
		}


		/// <summary>
		/// 改变状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = dwRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="dw"></param>
		/// <returns></returns>
		[HttpPost("AddDW")]
		public IActionResult AddDW(DW dw)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(dw.dw_code)
				|| string.IsNullOrEmpty(dw.dw_name)
				|| string.IsNullOrEmpty(dw.dw_type)
				|| string.IsNullOrEmpty(dw.dw_jx)
				|| string.IsNullOrEmpty(dw.dw_hsdw)
				|| string.IsNullOrEmpty(dw.dw_bl)
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = dwRepository.AddDW(dw);
			return Ok(res);
		}


		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="dw"></param>
		/// <returns></returns>
		[HttpPut("UpdateDW")]
		public IActionResult UpdateDW(DW dw)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(dw.dw_code)
				|| string.IsNullOrEmpty(dw.dw_name)
				|| string.IsNullOrEmpty(dw.dw_type)
				|| string.IsNullOrEmpty(dw.dw_jx)
				|| string.IsNullOrEmpty(dw.dw_hsdw)
				|| string.IsNullOrEmpty(dw.dw_bl)
				|| dw.dw_id <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = dwRepository.UpdateDW(dw);
			return Ok(res);
		}


		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelDW")]
		public IActionResult DelDW(int id)
		{
			bool res = dwRepository.DelDW(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelDWs")]
		public IActionResult DelDWs(int[] ids)
		{
			bool res = dwRepository.DelDWs(ids);
			return Ok(res);
		}

	}
}
